$(function () {
    $("#head").load("/html/head.html");
    $("#foot").load("/html/foot.html");
    selectNews();
})
function selectNews() {
    let data1 = {pageNumber: 1};
    let data = myAjax("/news",data1,"get");
    let list = data.list;
    let newslist = data.newslist;
    let newsClassifylist = data.newsClassifylist;
    let countPage = data.countPage;
    localStorage.setItem("countNewsPage",countPage)
    $("#main").attr("href", list[0].href);
    $("#main").text(list[0].title);
    localStorage.removeItem("typeNews");
    $("#now").attr("href", list[3].href);
    $("#now").text(list[3].title);
    let html1 ='';
    for (let i=0;i< newsClassifylist.length;i++){
        let type = i+1;
        html1 +='<div id="type'+type+'" onclick="selectNewsType('+type+')">'+ newsClassifylist[i].classify +'</div>';
    }
    $("#newsClassify").html(html1);
    let html2 ='';
    for (let i=0;i< newslist.length;i++){
        html2 +='<div class="newsMainAll">\n' +
            '                <div>\n' +
            '                    <div>'+newslist[i].dateRi+'</div>\n' +
            '                    <div>'+newslist[i].dateTian+'</div>\n' +
            '                </div>\n' +
            '                <div>\n' +
            '                    <div><a onclick="selectNewsById('+newslist[i].id+')" style="cursor:pointer">'+newslist[i].title+'</a></div>\n' +
            '                    <div>\n' +
            '                        '+newslist[i].content+'\n' +
            '                    </div>\n' +
            '                </div>\n' +
            '            </div>'
    }
    $("#newsdd").html(html2);

    //分页条
    let page = '';
    for (let i = 1; i <= countPage; i++) {
        page += '<div onclick="selectPage(' + i + ',' + countPage + ')" id="pageNumber' + i + '">' + i + '</div>'
    }
    $("#page").html(page);
    $("#pageNumber1").css("background-color", "black");
    $("#pageNumber1").css("color", "white");
}

//分种类查询
function selectNewsType (type) {
    let data = myAjax("/news", {type: type}, "get");
    localStorage.setItem("typeNews", type);
    let newslist = data.newslist;
    let countPage = data.countPage;
    let newsClassifylist = data.newsClassifylist;
    //将类别的信息的div变颜色
    for (let i = 1;i<=newsClassifylist.length;i++){
        $("#type"+i+"").css("background-color","black");
    }
    $("#type"+type+"").css("background-color","#0accce");

    localStorage.setItem("countNewsPage",countPage);
    let html2 ='';
    for (let i=0;i< newslist.length;i++){
        html2 +='<div class="newsMainAll">\n' +
            '                <div>\n' +
            '                    <div>'+newslist[i].dateRi+'</div>\n' +
            '                    <div>'+newslist[i].dateTian+'</div>\n' +
            '                </div>\n' +
            '                <div>\n' +
            '                    <div><a onclick="selectNewsById('+newslist[i].id+')" style="cursor:pointer">'+newslist[i].title+'</a></div>\n' +
            '                    <div>\n' +
            '                        '+newslist[i].content+'\n' +
            '                    </div>\n' +
            '                </div>\n' +
            '            </div>'
    }
    $("#newsdd").html(html2);

    let page = '';
    for (let i = 1; i <= countPage; i++) {
        page += '<div onclick="selectPage(' + i + ',' + countPage + ')" id="pageNumber' + i + '">' + i + '</div>'
    }
    $("#page").html(page);
    $("#pageNumber1").css("background-color", "black");
    $("#pageNumber1").css("color", "white");
}

function selectPage(i, n) {
    for (let t = 1; t <= n; t++) {
        $("#pageNumber" + t + "").css("background-color", "white");
        $("#pageNumber" + t + "").css("color", "black");
    }
    $("#pageNumber" + i + "").css("background-color", "black");
    $("#pageNumber" + i + "").css("color", "white");

    let type = localStorage.getItem("typeNews") == null ? 0 : localStorage.getItem("typeNews");
    let data = myAjax("/news", {pageNumber: i, type: type}, "get");
    let newslist = data.newslist;
    let html2 ='';
    for (let i=0;i< newslist.length;i++){
        html2 +='<div class="newsMainAll">\n' +
            '                <div>\n' +
            '                    <div>'+newslist[i].dateRi+'</div>\n' +
            '                    <div>'+newslist[i].dateTian+'</div>\n' +
            '                </div>\n' +
            '                <div>\n' +
            '                    <div><a href="#">'+newslist[i].title+'</a></div>\n' +
            '                    <div>\n' +
            '                        '+newslist[i].content+'\n' +
            '                    </div>\n' +
            '                </div>\n' +
            '            </div>'
    }
    $("#newsdd").html(html2);
}

function firstPage() {
    let countPage = localStorage.getItem("countNewsPage");
    selectPage(1, countPage);
}

function endPage() {
    let countPage = localStorage.getItem("countNewsPage");
    selectPage(countPage, countPage);
}
//查询一条数据的详细内容
function selectNewsById(id) {
    let data = myAjax('/news/findById',{id:id},'get');
    data = data.model;
    let html ='<div class="newsMain">' +
        '    <!--    title-->' +
        '       <div>'+data.title+'</div>' +
        '    <!--    来源-->' +
        '       <div>' +
        '          <span>来源:</span>' +
        '          <span>作者：'+data.author+'</span>' +
        '          <span>发布时间：'+data.time+'</span>' +
        '          <span>'+data.visit+'次浏览</span>' +
        '       </div>' +
        '    <!--    content-->' +
        '       <div>'+data.content+'</div>' +
        '    <!--    contentall-->' +
        '       <div>'+data.contentall+'</div>' +
        '</div>';
    $("#newsdd").html(html);
    let page = '';
    $("#pageAll").html(page);
}
